/**
 * @description: 获取 店铺账号信息  快手
 * @return {*}
 */
async function getKSUserInfo(shopId) {
  const url = "https://s.kwaixiaodian.com/rest/app/tts/shop/baseInfo/get";
  const res = await request(globalData.source, url, {shopId}, 'post');
  if(res.result === 1 && res.data){
    return res.data
  }else{
    return false
  }
}


/**
 * @description: 获取当天解密数据  快手
 * @return {*}
 */
async function ksApiGetQuota() {
  const url = "https://s.kwaixiaodian.com/gateway/business/invite/quota/get";
  const res = await request(globalData.source, url);
  const quota = {
    used_quota: 0,
    unused_quota: 0,
  };
  let canApply = false
  let applicationNum = 0
  if (res.result === 1 && res.data) {
    const { quotaCost: used_quota = 0, quotaRemain: unused_quota = 0, quotaProgress: {canApply: canApply_ = false}, targetQuotas: targetQuotas = [] } =
      res.data;
    quota["used_quota"] = used_quota;
    quota["unused_quota"] = unused_quota;
    canApply = canApply_
    if(targetQuotas.length){
      applicationNum = targetQuotas.reduce((pre, cur) => {
        if(cur.targetQuota > pre){
          pre = cur.targetQuota
        }
        return pre
      }, 0)
    }
  }else if(res.result === 109){
    window.history.go(0)
  }
  return {quota, canApply, applicationNum};
}



/**
 * @description: 获取当天解密数据  抖店
 * @param {*} token
 * @return {*}
 */
async function ddApiGetQuota(token) {
  // const url =
  //   "https://fxg.jinritemai.com/api/order/receiveInfoViewQuotaInfo?appid=1&__token=" +
  //   token +
  //   "&_bid=ffa_order&aid=4272&_lid=554558261428&msToken=Jz00gLvr72OMfhMzUw6xozwtV9SqpAtJecAKgHqBzPymBUd8b6c6FVRRPGxHuiIftm2sHIoV4yIw9-hUxX4YQgM2afpKFwlq-LWxCrzEKU5a04ihUa2zlQ==&X-Bogus=DFSzswVuQpvAN9jsSpw3ykH/VyCQ&_signature=_02B4Z6wo00001RY2SrwAAIDCF35lcvk1i3EWNk4AACYQzVrWbEOx4E1MB76Uytz345MhmvOJLAUwmelD4.qNPoZwbVDjLUwHaP1Kj8jN.-SR.zK2RmdgfSzlnaBOtPwoBWXhLp09qMYL.pUze3";
  const url = 'https://fxg.jinritemai.com/shopuser/govern/bff/api/govern-privacy-center/getQuotaManageDecryptCnt'
  const res = await request(globalData.source, url);
  if (res.code === 0 && res.QuotaList && res.QuotaList.length) {
    return res.QuotaList.reduce((pre, cur) => {
      const {QuotaName, QuotaValue} = cur
      if(QuotaName === 'remainDecrptQuota'){
        pre['unused_quota'] = QuotaValue
      }else if(QuotaName === 'alreadyDecrptQuota'){
        pre['used_quota'] = QuotaValue
      }
      return pre
    }, {})
  } else {
    return {
      used_quota: 0,
      unused_quota: 0,
    };
  }
}

/**
 * @description: 获取审核结果  快手
 * @return {*}
 */
async function ksApiGetQuotaApplyList() {
  const url =
    "https://s.kwaixiaodian.com/gateway/business/invite/quota/list?pageSize=10&pageNum=1&startTime=&endTime=";
  const res = await request(globalData.source, url);
  // 
  if (res.result === 1 && res.data.length) {
    return res.data[0].status;
  } else {
    return false;
  }
}

/**
 * @description: 获取审核结果  抖店
 * @param {*} token
 * @return {*}
 */
async function ddApiGetQuotaApplyList(token) {
  const url =
    "https://fxg.jinritemai.com/api/order/receiveInfoViewQuotaApplyHistory?page=0&pageSize=10&appid=1&__token=" +
    token +
    "&_bid=ffa_order&aid=4272&_lid=554558261428&msToken=Jz00gLvr72OMfhMzUw6xozwtV9SqpAtJecAKgHqBzPymBUd8b6c6FVRRPGxHuiIftm2sHIoV4yIw9-hUxX4YQgM2afpKFwlq-LWxCrzEKU5a04ihUa2zlQ==&X-Bogus=DFSzswVuQpvAN9jsSpw3ykH/VyCQ&_signature=_02B4Z6wo00001RY2SrwAAIDCF35lcvk1i3EWNk4AACYQzVrWbEOx4E1MB76Uytz345MhmvOJLAUwmelD4.qNPoZwbVDjLUwHaP1Kj8jN.-SR.zK2RmdgfSzlnaBOtPwoBWXhLp09qMYL.pUze3";
  const res = await request(globalData.source, url);
  // 0 审核中 1 审核通过 2 审核不通过
  if (res.code === 0 && res.data.length) {
    if(res.data[0].task_status === 2){
      if(res.data.length > 1){
        if(res.data[1].task_status === 2){
          const date_1 = new Date(res.data[0].apply_time).toDateString()
          const date_2 = new Date(res.data[1].apply_time).toDateString()
          const date_cur = new Date().toDateString()
          // 一天内最近连续两次申请审核失败后不再申请
          if(date_1 === date_2 && date_2 === date_cur){
            return 3
          }else{
            return res.data[0].task_status;
          }
        }else{
          return res.data[0].task_status;
        }
      }else{
        return res.data[0].task_status;
      }
    }else{
      return res.data[0].task_status;
    }
  } else {
    return false;
  }
}

/**
 * @description: 获取是否已达申请上限  抖店
 * @param {*} token
 * @return {*}
 */
 async function ddApiReceiveInfoViewQuotaApplyInfo(token) {
    const url = "https://fxg.jinritemai.com/api/order/receiveInfoViewQuotaApplyInfo?apply_source=PC_ACTIVE&appid=1&__token="+token+"&_bid=ffa_order&aid=4272&_lid=585970209409&msToken=cBH9URTUtNPLSkT9yu7P4tkvX7Cpe3K871Yfr0vEMAe5rjFPw_XPi3Q9z7C_dx5TgUrNB6XLzoMhSsQtdYER68TYijL7PdpzP5K16zNYsqukFG4nhYfMIryvjQvZavo=&X-Bogus=DFSzswVYkb0ANx7XSpwE5iH/Vyeo&_signature=_02B4Z6wo00001qzNYygAAIDBrYVM5Pmd5u6szWeAAMi.Yecb50VEOnRw-8oNsQBvJEnhmtk-7Xfasr6b0Y3ytpCNXYLb3KRixMcfb-yqlVCulo6.9IZINcAz3X.gYi7i10JLY8LtJQ4Wmy.E9c";
    const res = await request(globalData.source, url);
    if(res.code === 0 && res.data){
      return res.data
    }else{
      return false
    }
    // apply_status  0    apply_status 2 申请达到上限
  }



  /**
 * @description: 获取本次最大可提额数  抖店
 * @param {*} token
 * @return {*}
 */
 async function getReceiveInfoViewQuotaInfo() {
  const url = "https://fxg.jinritemai.com/shopuser/govern/bff/api/govern-privacy-center/getReceiveInfoViewQuotaInfo";
  const res = await request(globalData.source, url);

  if(res.code === 0 && res.canApplyQuota){
    return Number(res.canApplyQuota)
  }else{
    return false
  }
}


  /**
 * @description: 获取本次最大可提额数  抖店
 * @param {*} token
 * @return {*}
 */
   async function getApplyQuotaReason() {
    const url = "https://fxg.jinritemai.com/shopuser/govern/bff/api/govern-privacy-center/getApplyQuotaReason";
    const res = await request(globalData.source, url);
  
    if(res.code === 0 && res.applyReasons.length){
      return res.applyReasons.map((label, value) => {
        return {label, value}
      })
    }else{
      return false
    }
  }

  

  



  /**
 * @description: 获取提额权限  抖店
 * @param {*} token
 * @return {*}
 */
 async function ddGetReceiveInfoViewQuotaApplyInfo() {
  const url = "https://fxg.jinritemai.com/shopuser/govern/bff/api/govern-privacy-center/getReceiveInfoViewQuotaApplyInfo";
  const res = await request(globalData.source, url);
  // ApplydeQuotaTimes  今日剩余提额次数
  // Status  2  解密额度已冻结，不可申请
  if(res.code === 0){
    return res
  }else if(res.code == 10008){
    window.history.go(0)
  }else{
    return false
  }
}




  /**
 * @description: 申请提额  快手
 * @return {*}
 */
async function applyQuotaKS(params) {
  const url =
    "https://s.kwaixiaodian.com/gateway/business/invite/quota/apply";
    // 调试
    // return true

  const res = await request(globalData.source, url, params, 'post');
  // 
  if (res.result === 1 && res.data) {
    return res.data;
  } else {
    return false;
  }
}


  /**
 * @description: 申请提额  抖店
 * @return {*}
 */
   async function applyQuotaDD(params) {
    const url = "https://fxg.jinritemai.com/shopuser/govern/bff/api/govern-privacy-center/postReceiveInfoViewQuotaApply";
    // 调试
    // return true
    const res = await request(globalData.source, url, params, 'post');
    
    if (res.code === 0) {
      return true;
    } else {
      return false;
    }
  }





/**
 * @description: 获取当前账号的权限
 * @param {*} source 店铺类型  10 快手   7 抖店
 * @param {*} id 店铺名称(快手) / 店铺id（抖店）
 * @return {*}
 */
async function getAccountAuth(source, id) {
  const baseURL = 'https://api.scrm.keyid.cn'
  const url = "/scrm-api/console/retail/shop/get-shop-status";
  let data = {source}
  switch(source){
    case 10:
      data['shop_name'] = id
    break;
    case 7:
      data['external_shop_id'] = id
    break;
  }
    const res = await request(baseURL, url, data, 'post');
    if(res.code === 0 && res.data){
      return res.data
    }else{
      return false
    }
}
